# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit eutils

S="${WORKDIR}/windows-1251"

SLOT="0"
IUSE="X"
DESCRIPTION="Collection of cyrillic raster fonts for X Window System by D. Bolkhovityanov."
HOMEPAGE=
SRC_URI="${P}.bdfs.tar.bz2"
LICENSE="distributable"
KEYWORDS="alpha amd64 arm hppa ia64 mips ppc ppc64 s390 sh sparc x86 ~x86-fbsd"
RDEPEND=""
DEPEND="${RDEPEND}
	x11-apps/bdftopcf
	media-fonts/font-util"

FONT_ROOT=/usr/share/fonts/cyr-rfx-cp1251
FONT_TARGETS="75dpi misc"

src_unpack() {
    unpack ${A}
    cd ${S}
    
    perl -pi -e 's/-Adobe/-rfx/' 75dpi/*.bdf
    perl -pi -e 's/\"Adobe/\"rfx/' 75dpi/*.bdf
    perl -pi -e 's/windows/microsoft/' 75dpi/*.bdf
    perl -pi -e 's/-1251/-cp1251/' 75dpi/*.bdf

    perl -pi -e 's/-Adobe/-rfx/' misc/*.bdf
    perl -pi -e 's/\"Adobe/\"rfx/' misc/*.bdf
    perl -pi -e 's/windows/microsoft/' misc/*.bdf
    perl -pi -e 's/-1251/-cp1251/' misc/*.bdf

    cp ${FILESDIR}/Makefile.fonts ./Makefile
    cp ${FILESDIR}/fonts.alias.misc.1251 ./misc/fonts.alias
    cp ${FILESDIR}/fonts.alias.75dpi ./75dpi/fonts.alias

}

src_compile() {
    emake || die "emake failed"
}

src_install() {
    emake DESTDIR="${D}" install || die "emake install failed"
    dodoc doc/*
}

rebuild_fontfiles() {
    einfo "Refreshing fonts.scale and fonts.dir..."
    cd ${FONT_ROOT}
    mkfontdir -- ${FONT_TARGETS}
    if [ "${ROOT}" = "/" ] &&  [ -x /usr/bin/fc-cache ]
    then
	einfo "Updating font cache..."
	HOME="/root" /usr/bin/fc-cache -f ${FONT_TARGETS}
    fi
}

pkg_postinst() {
    if use X > /dev/null ; then
	rebuild_fontfiles
	einfo "In order to make X see the fonts, you need to add the"
	einfo "following lines to the files section of your "
	einfo "/etc/X11/XF86Config (or the XOrg equivalent):"
	einfo "  FontPath \"${FONT_ROOT}/100dpi/:unscaled\""
	einfo "  FontPath \"${FONT_ROOT}/misc/:unscaled\""
	einfo "  FontPath \"${FONT_ROOT}/75dpi/:unscaled\""
	einfo ""
	einfo "In order to make them accessible to your current X"
	einfo "session, run:"
	einfo "  xset fp+ ${FONT_ROOT}/100dpi/:unscaled,\\"
	einfo "${FONT_ROOT}/misc/:unscaled,\\"
	einfo "${FONT_ROOT}/75dpi/:unscaled"
	einfo "  xset fp rehash"
    fi
}

pkg_postrm() {
    if use X > /dev/null ; then
	rebuild_fontfiles
    fi
}
